package nettyTest.netty3Test.codecTest1.codeTest1;

import java.util.List;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageCodec;

/**
 * @author Administrator encode Integer ==> String decode String ==> Integer
 * String这种还是用原生的docode
 */
public class MyMessageToMessageCodec extends MessageToMessageCodec<String, Integer> {

	/**
	 * decode 会根据接受的数据，调用多次，知道确定没有新的元素添加到list或者Bytebuf没有更多的读字节为止，如果list out不为空，
	 * 就将list传递给下一个channelInboundHandler处理，该处理器方法也会调用多次。
	 * @param ctx
	 * @param msg
	 * @param out
	 * @throws Exception
	 */

	@Override
	protected void encode(ChannelHandlerContext ctx, Integer msg, List<Object> out) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("========***************MyMessageToMessageCodec2 encode Integer msg " + msg);
		out.add(msg);
	}

	@Override
	protected void decode(ChannelHandlerContext ctx, String msg, List<Object> out) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("========***************MyMessageToMessageCodec2 decode String to Integer " + msg);
		out.add(Integer.valueOf(msg));
	}

}
